package com.lw.kotlin_mvvm_master.view.navigation

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.lw.common.base.BaseActivity
import com.lw.kotlin_mvvm_master.R
import com.lw.kotlin_mvvm_master.databinding.ActivityNavigationBinding
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView.OnPagerTitleChangeListener

class NavigationActivity : BaseActivity<ActivityNavigationBinding>() {
    private lateinit var mainAdapter: MainAdapter
    private var fragments = mutableListOf<MainMenuModel>()

    override fun initView(savedInstanceState: Bundle?) {
        fragments.add(MainMenuModel("首页", R.mipmap.pm_icon_home_off, R.mipmap.pm_icon_home_on, HomeFragment()))
        fragments.add(MainMenuModel("我的", R.mipmap.pm_icon_mine_off, R.mipmap.pm_icon_mine_on, HomeFragment()))
        initMagicIndicator()
    }

    private fun initMagicIndicator() {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.isAdjustMode = true
        mainAdapter = MainAdapter(fragments)
        commonNavigator.adapter = mainAdapter
        mainAdapter.setItemClickListener(object : MainAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                mBinding.viewpager.setCurrentItem(position, false)
            }
        })
        mBinding.tabLayout.navigator = commonNavigator
//        禁止滑动
        mBinding.viewpager.isUserInputEnabled = false
        mBinding.viewpager.adapter = object : FragmentStateAdapter(this) {
            override fun createFragment(position: Int): Fragment {
                return fragments[position].fragment
            }

            override fun getItemCount(): Int {
                return fragments.size
            }
        }
        mBinding.viewpager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                mBinding.tabLayout.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                mBinding.tabLayout.onPageSelected(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
                mBinding.tabLayout.onPageScrollStateChanged(state)
            }
        })
    }
}

class MainAdapter(private val fragments: MutableList<MainMenuModel>) : CommonNavigatorAdapter() {
    override fun getCount(): Int {
        return fragments.size
    }

    @SuppressLint("MissingInflatedId,InflateParams")
    override fun getTitleView(context: Context, index: Int): IPagerTitleView {
        val titleView = CommonPagerTitleView(context)
        // 底部导航栏自定义样式
        val customLayout: View = LayoutInflater.from(context).inflate(R.layout.adapter_navigator, null)
        val titleImg = customLayout.findViewById<ImageView>(R.id.title_img)
        val titleText = customLayout.findViewById<TextView>(R.id.title_text)
        val titleCount = customLayout.findViewById<TextView>(R.id.title_count)
        titleText.text = fragments[index].title
        if (fragments[index].title == "待办" && badgeCount != "") {
            titleCount.isVisible = true
            titleCount.text = badgeCount
        } else {
            titleCount.isVisible = false
        }
        titleView.setContentView(customLayout)
        titleView.onPagerTitleChangeListener = object : OnPagerTitleChangeListener {
            override fun onSelected(index: Int, totalCount: Int) {
                titleText.setTextColor(Color.parseColor("#4378F6"))
                titleImg.setImageResource(fragments[index].iconSelect)
            }

            override fun onDeselected(index: Int, totalCount: Int) {
                titleText.setTextColor(Color.parseColor("#A3B4CC"))
                titleImg.setImageResource(fragments[index].iconNormal)
            }

            override fun onLeave(index: Int, totalCount: Int, leavePercent: Float, leftToRight: Boolean) {
                ViewCompat.animate(titleView)
                    .scaleX(1.0f + (0.9f - 1.0f) * leavePercent)
                    .scaleY(1.0f + (0.9f - 1.0f) * leavePercent)
                    .setDuration(300)
                    .setInterpolator(DecelerateInterpolator())
                    .start()
            }

            override fun onEnter(index: Int, totalCount: Int, enterPercent: Float, leftToRight: Boolean) {
                ViewCompat.animate(titleView)
                    .scaleX(0.9f + (1.0f - 0.9f) * enterPercent)
                    .scaleY(0.9f + (1.0f - 0.9f) * enterPercent)
                    .setDuration(300)
                    .setInterpolator(DecelerateInterpolator())
                    .start()
            }
        }
        titleView.setOnClickListener {
            mOnItemClickListener!!.onItemClick(index)
        }
        return titleView
    }

    override fun getIndicator(context: Context): IPagerIndicator? {
        return null
    }

    private var badgeCount = ""
    fun setBadgeCount(badgeCount: String) {
        this.badgeCount = badgeCount
        notifyDataSetChanged()
    }

    // 点击事件接口
    private var mOnItemClickListener: OnItemClickListener? = null

    // 设置点击事件监听器
    fun setItemClickListener(listener: OnItemClickListener?) {
        mOnItemClickListener = listener
    }

    // 点击事件接口定义
    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }
}

class MainMenuModel(
    var title: String,
    var iconNormal: Int,
    var iconSelect: Int,
    var fragment: Fragment,
)